﻿// Entity Framework MapTileStore - http://maptilestore.codeplex.com/
// Copyright © 2012 Majid Qaraee
// Licensed under the Microsoft Public License (Ms-PL)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows;
using System.ComponentModel;

namespace MapEntity.Model
{
    public class TileType : INotifyPropertyChanged
    {
        public TileType()
        {
        }
        
        public string SourceName { get; set; }
        public byte[] Image { get; set; }

        private int zoomLevel;

        public int ZoomLevel
        {
            get { return zoomLevel; }
            set
            {
                zoomLevel = value;
                MapApi mapApi = new MapApi(value);
                x = mapApi.LongitudeToXTile(longitude);
                y = mapApi.LatitudeToYTile(latitude);
                NotifyPropertyChanged("ZoomLevel");
            }
        }
        
        private int x;

        public int X
        {
            get { return x; }
            set
            {
                x = value;
                NotifyPropertyChanged("X");
            }
        }

        private int y;

        public int Y
        {
            get { return y; }
            set
            {
                y = value;
                NotifyPropertyChanged("Y");
            }
        }


        private double longitude;

        public double Longitude
        {
            get { return longitude; }
            set
            {
                longitude = value;
                MapApi mapApi = new MapApi(ZoomLevel);
                x = mapApi.LongitudeToXTile(value);
                NotifyPropertyChanged("Longitude");
            }
        }

        private double latitude;

        public double Latitude
        {
            get { return latitude; }
            set
            {
                latitude = value;
                MapApi mapApi = new MapApi(ZoomLevel);
                y = mapApi.LatitudeToYTile(value);
                NotifyPropertyChanged("Latitude");
            }
        }


        public static TileType Pars(string key)
        {
            string[] fields = key.Split('/');
            try
            {
                var tile = new TileType()
                {
                    SourceName = fields[0],
                    ZoomLevel = int.Parse(fields[1]),
                    X = int.Parse(fields[2]),
                    Y = int.Parse(fields[3])
                };

                return tile;
            }
            catch
            {
                return null;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }
}
